<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>对props进行限制</title>
</head>
<body>
  <div id="test"></div>
  <div id="test1"></div>
  <div id="test2"></div>
  <div id="test3"></div>

  <!-- 引入库：先引入核心库，再引入操作DOM的扩展库 -->
  <script type="text/javascript" src="../js/react.development.js"></script>
  <script type="text/javascript" src="../js/react-dom.development.js"></script>
  <!-- 将jsx转为js -->
  <script type="text/javascript" src="../js/babel.min.js"></script>
  <!-- 引入prop-types，用于对组件标签属性进行限制 -->
   <script type="text/javascript" src="../js/prop-types.js"></script>

  <script type="text/babel">    //必须写babel，因为默认js

    class Person extends React.Component{
      render(){
        const {name,age,sex} = this.props
        return (
          <ul>
            <li>姓名：{name}</li>
            <li>年龄：{age+1}</li>
            <li>性别：{sex}</li>
          </ul>
        )
      }
    }

    //对标签属性进行类型、必要性的限制
    Person.propTypes = {
      name:PropTypes.string.isRequired,     //string:限制字符串类型，isRequired:限制必须传
      sex:PropTypes.string,
      age:PropTypes.number,                 //限制为Number类型
      speak:PropTypes.func                  //限制为函数类型
    }
    //对标签属性进行默认值
    Person.defaultProps = {
      sex:'不男不女',
      age:18
    }

    //少量数据，提示只有js才会由Number，jsx需要用{}包裹才是Number类型
    ReactDOM.render(<Person name='zhang' age={19} sex='男' speak={speak}/>, document.getElementById('test'))
    ReactDOM.render(<Person name='王'/>, document.getElementById('test1'))
    
    //大数据
    const p = {name:'li',age:23,sex:'女'}
    ReactDOM.render(<Person name={p.name} age={p.age} sex={p.sex}/>, document.getElementById('test2'))

    //大数据语法糖，批量处理，前提数据传输的key命名和类中一样
    ReactDOM.render(<Person {...p}/>, document.getElementById('test3'))

    function speak(){
      console.log('我要开始说话了')
    }
  </script>
</body>
</html>